/* 引入less */
/* 重置样式 */
require('../css/commCss/normalize.css')
/* 重置样式补充 */
require('../css/commCss/reset.css')
/* 公共样式 */
require('../css/commCss/property.css')
/* 公共属性 */
require('../css/commCss/basic.less')
/* 字体图标 */
require('../fonts/iconfont.css')
/* 轮播图 */
require('../css/swiper/swiper-bundle.css')
/* 当前页面 */
require('../css/home.less')

/**
 * 获取定义数据
 */
//轮播图
const Swiper = require('../css/swiper/swiper-bundle')
//ajax
const axios = require('axios')
//weui-插件库
require('weui')
const weui = require('weui.js')
const util = require('../util/util')


document.addEventListener('DOMContentLoaded', function () {
    /**
     * 获取变量 
     */
    const rank = document.querySelector('#rank')
    const rankDay = document.querySelector('#rankDay')
    const isPunch = document.querySelector('#isPunch')
    const insigniaNum = document.querySelector('#insigniaNum')


    /**
     * 1. 轮播图
     */
    const mySwiper = new Swiper('.swiper', {
        loop: true,
        autoplay: {
            delay: 2500, //循环间隔时间
            stopOnLastSlide: true, //触碰图片后否继续循环
            disableOnInteraction: true,
        },
        pagination: {
            el: '.swiper-pagination',
        },
    })
    /**
     * 2. 连接服务器 渲染数据
     */

    let localUserId = localStorage.getItem('userId')
    axios.get(`http://139.9.177.51:8099/headPageInfo?userId=${localUserId}`, {
        parmas: {
            userId: localStorage.getItem('userId')
        }
    }).then(function (res) {
        rank.textContent = res.data.data.rank
        rankDay.textContent = res.data.data.punchIn
        insigniaNum.textContent = res.data.data.insigniaNum

        res.data.data.isPunch === 'true' ? isPunch.textContent = '已打卡' : isPunch.textContent = '今日打卡'
    })

    /**
     * 3. 打卡
     */
    isPunch.addEventListener('click', function () {
        /* console.log(localStorage.getItem('userId')); */
        axios.get(`http://139.9.177.51:8099/clockIn?userId=${localUserId}`, {
            params: {
                userId: localStorage.getItem('userId')
            }
        }).then(function (res) {
            /* console.log(res); */
            if (res.data.status === 0) {
                rankDay.textContent++
                isPunch.textContent = '已打卡'
                weui.toast('打卡成功', 1000)
            } else {
                /* isPunch.textContent = '今日打卡' */
                weui.toast(`${res.data.msg}`, 1000)
            }
        })
    })

    /* 点击运动数据板块跳转页面 */
    document.querySelector("#leftData").addEventListener("click",function(){
        location.href = "./motionData.html"
    })

    /**
     * 4. 底部导航栏
     */
    util.utilBottomNav('home', 'app-home')
})